<?

$DATA['structmap'] = datafetch('structmap');
$res = array();

function getElement($a, $data=0) {
	global $DATA;
	
	if (!is_array($data)) $data = $DATA['structmap'];
	
	foreach($data as $p)
		if ($p['id'] == $a) return $p; else
			if (isset($p['data'])) 
				if ($res = getElement($a, $p['data'])) return $res;

	return false;
}


// tasks handling


if (($a = (int)param('add')) && $s = getElement($a)) {
	
	if ($s['tag'] & PT_NODE) {

		$tag = ($s['tag'] & PT_MAIN) ? PT_NODE : 0;

		$max = ($m = datafetch('db_item', tb('struct'), "parent=$a AND ".dbAndL('tag', PT_CONFIGURABLE), 'MAX(pos) as m')) ? $m['m']+1 : 1;

		$id = datafetch('db_insert', tb('struct'), array('parent' => $a, 
				'attr' => 0, 
				'tag' => $tag | PT_ADMINABLE | PT_EDITABLE | PT_MAPABLE | PT_CONFIGURABLE | PT_NODE,
				'name' => "New page", 
				'keyword' => 'test',
				'pos' => $max,
				'template' => 'main-multiblock',				
				'admin' => 'adm-multiblock',
				'code' => 'admparse:multiblock, crumb:edit',
				'kind' => 0,
			));
		
	}

	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'add', 'add_param' => "edit:$id")));
	exit;
}

if (($a = (int)param('delete')) && $s = getElement($a)) {		// !!! cleanup for simpletext only

	if (!($s['tag'] & (PT_MAIN | PT_SPECIAL | PT_LOCKED))) {

		switch ($s['template']) {		//cleanup
		
			case 'main-simpletext':
				dataset('db_delete', tb('article'), "id_page=$a");
				break;
		}

		dataset('db_delete', tb('struct'), "id=$a");
		exit;		// ajax processing done
	}
	
	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
	exit;
}
	
	
if (($a = (int)param('show')) && ($s = getElement($a)) && !($s['tag'] & PT_LOCKED) ) {
	
	dataset('db_update', tb('struct'), "attr = ".dbOr('attr', AA_VISIBLE), "id=$a");

	exit;
}

if (($a = (int)param('hide')) && ($s = getElement($a)) && !($s['tag'] & PT_LOCKED) ) {
	
	dataset('db_update', tb('struct'), "attr = ".dbAnd('attr', ~AA_VISIBLE), "id=$a");

	exit;
}

if ($a = strtolower(trim(param('sort')))) {
	
	switch($a) {
	
		case 'manual':	// ajax request, no data should be returned

//			ob_start();

			$t = 1;

			if ($p = (int)postdata('parent')) {

				$DATA['objects'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb('struct'), 'where' => "parent=$p",	'order' => 'pos', 'index' => 'id' )));

				parse_str(postdata('order'), $temp);
				
				if (is_array($temp = $temp["s$p"]) && sizeof($temp)) {
//dbdebug();
//debug($temp);
					foreach($temp as $id) {
						if ($id = (int)$id) {
							if ($DATA['objects'][$id]) {
								dataset('db_update', tb('struct'), "pos = ".$t++, "parent=$p AND id=$id");
								unset($DATA['objects'][$id]);
							}
						}
					}
					
					if (sizeof($DATA['objects']))
						foreach($DATA['objects'] as $id => $v) 
							dataset('db_update', tb('struct'), "pos = ".$t++, "parent=$p AND id=$id");
				}
				
//				$_res['diags'] = ob_get_clean();
//				print(php2js($_res));
				
				exit;	// ajax: no more processing needed
				break;
			}
}
	
	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'sort')));
	exit;
}
	


?>